/*
 * Copyright 2009-2010 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.filesystem.win32.proxy;

import java.io.File;

import javax.swing.Icon;

import org.kommando.filesystem.FileSystemBrowser;
import org.kommando.filesystem.FileSystemBrowserException;
import org.kommando.filesystem.FileSystemCatalogObjectFactory;
import org.kommando.filesystem.core.FileSystemObject;
import org.kommando.proxyobjects.ProxyObject;

/**
 * Selected file {@link ProxyObject}, uses {@link FileSystemBrowser} to get the currently selected file.
 * 
 * @author Peter De Bruycker
 */
public class SelectedFileProxyObject implements ProxyObject {

    private FileSystemBrowser browser;

    public SelectedFileProxyObject(FileSystemBrowser browser) {
        this.browser = browser;
    }

    @Override
    public <T> T getExtension(Class<T> extensionType) {
        try {
            String selectedFile = browser.getSelectedFile();

            if (selectedFile == null) {
                return null;
            }

            return extensionType.cast(FileSystemCatalogObjectFactory.createFileObject(new File(selectedFile)));

        } catch (FileSystemBrowserException e) {
            // TODO log error
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Class<?> getExtensionType() {
        return FileSystemObject.class;
    }

    @Override
    public Icon getIcon() {
        // TODO icon
        return null;
    }

    @Override
    public String getName() {
        return "Selected File";
    }
}
